package com.fjzn.detect.config.conmmo;

import com.fasterxml.jackson.databind.module.SimpleModule;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDateTime;

@Configuration
public class LocalDateTimeJsonConfig {

    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
        return builder -> {
            builder.serializerByType(LocalDateTime.class, new LocalDateTimeSerializer());
            builder.deserializerByType(LocalDateTime.class,new LocalDateTimeDeserializer());
        };
    }

    @Bean
    public com.fasterxml.jackson.databind.Module customeJackSonModule() {
        SimpleModule bean = new SimpleModule();
        //LocalDateTime的反系列化使用自定义的处理器，用于适应多种自定义日期格式的转换处理
        bean.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer());
        //String的反系列化使用自定义的处理器，用于XSS攻击
        //bean.addDeserializer(String.class, MyStringDeserializer.INSTANCE);
        //LocalDateTime的系列化替换默认的ISO日期格式为自定义的日期格式
        bean.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer());
        return bean;
    }

}
